<?php

use App\Models\User;
use Predis\Client;
use Yaf\Registry;
use OpenApi\Annotations as OA;

/**
 * @OA\Info(
 *     title="My First API",
 *     version="1.0.0"
 * )
 * @OA\Server(
 *     url="http://localhost/api",
 *     description="Local server"
 * )
 */
class IndexController extends \App\controllers\BaseController {
    /**
     * 默认初始化方法，如果不需要，可以删除掉这个方法
     * 如果这个方法被定义，那么在Controller被构造以后，Yaf会调用这个方法
     */
    public function init() {
//		$this->getView()->assign("header", "Yaf Example");
        Yaf\Dispatcher::getInstance()->autoRender(false);
    }

    /**
     * @OA\Get(
     *     path="/api/article",
     *     tags={"文章管理"},
     *     summary="文章列表",
     *     @OA\Parameter(
     *         name="token",
     *         in="header",
     *         description="token",
     *         @OA\Schema(type="string", default="123456")
     *     ),
     *     @OA\Parameter(
     *         name="page",
     *         in="query",
     *         description="页码",
     *         @OA\Schema(type="int", default="1")
     *     ),
     *     @OA\Parameter(
     *         name="limit",
     *         in="query",
     *         description="行数",
     *         @OA\Schema(type="int", default="10")
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="The User"
     *     )
     * )
     */
	public function indexAction($name = "Stranger") {
        // 获取请求对象
        $data = [
            'message' => 'Hello, Yaf!',
            'time'    => date('Y-m-d H:i:s'),
        ];

        // 返回数据即可，插件会自动处理 JSON 封装
        // 将数据设置到响应对象中

        // 使用 Predis 操作 Redis
        $client = new Client([
            'scheme' => 'tcp',
            'host'   => '127.0.0.1',
            'password'   => '123456',
            'port'   => 6379,
        ]);

        $client->set('foo', 'bar');
        $value = $client->get('foo');


        $users = User::query()->where('id', 1)->first();
        $users = User::query()->where('id', '>',1)->first();
        return $this->respond($users);
		//1. fetch query
		$get = $this->getRequest()->getQuery("get", "default value");

		//2. fetch model
		$model = new SampleModel();

		//3. assign
		$this->getView()->assign("content", $model->selectSample());
		$this->getView()->assign("name", $name);

		//4. render by Yaf, 如果这里返回FALSE, Yaf将不会调用自动视图引擎Render模板
        return TRUE;
	}

    /**
     * @OA\Get(
     *     path="/user/{id}",
     *     summary="获取用户信息",
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="用户ID",
     *         required=true,
     *         @OA\Schema(type="integer")
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="成功",
     *         @OA\JsonContent(
     *             type="object",
     *             @OA\Property(property="id", type="integer"),
     *             @OA\Property(property="name", type="string")
     *         )
     *     ),
     *     @OA\Response(
     *         response=404,
     *         description="用户不存在"
     *     )
     * )
     */
    public function testAction(){
        echo "cli success";
        $logger = \Yaf\Registry::get('logger');
        $logger->info("cli success");
    }

    public function swaggerAction()
    {
        $openapi = \OpenApi\scan(APPLICATION_PATH. '/application/controllers');

        echo "<pre>";print_r($openapi);exit;
        header('Content-Type: application/json');
        echo $openapi->toJson();
    }
}
